package cn.fanyc.action.mq.kafka.producer.configuration;

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaAdmin;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;

/**
 * @author Fanyc
 * @date 2020-06-29 16:00
 */
@Configuration
public class KafkaConfiguration {

    @Autowired
    private KafkaAdmin kafkaAdmin;

    @Bean
    public void createTopics() {
        AdminClient adminClient = AdminClient.create(kafkaAdmin.getConfigurationProperties());
        try {
            System.out.println(adminClient.listTopics().names().get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        NewTopic topic1 = new NewTopic("topic1", 4, (short) 2);
        NewTopic topic2 = new NewTopic("topic2", 3, (short) 3);
        NewTopic topic3 = new NewTopic("topic3", 5, (short) 2);
        adminClient.createTopics(Arrays.asList(topic1,topic2,topic3));
    }
}
